﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace linq2xml
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateXMLDocByDOM();
        }

        static void CreateXMLDocByDOM()
        {
            XmlDocument doc = new XmlDocument();//创建 XmlDocument 对象表示 XML 文档
            XmlElement bookLst = doc.CreateElement("BookList");
            //创建 BookList 根结点
            XmlElement book, auth;
            book = doc.CreateElement("Book"); //创建并添加第 1 个子结点 Book-1 
            book.SetAttribute("Name", "Book-1");
            auth = doc.CreateElement("Author");
            auth.InnerText = "Author-1";
            book.AppendChild(auth);
            bookLst.AppendChild(book);
            book = doc.CreateElement("Book"); //创建并添加第 2 个子结点 Book-2 
            book.SetAttribute("Name", "Book-2");
            auth = doc.CreateElement("Author");
            auth.InnerText = "Author-2";
            book.AppendChild(auth);
            bookLst.AppendChild(book);
            doc.AppendChild(bookLst); //添加第一个 BookList 子结点到 XML 文档
            doc.Save("f1.xml"); //保存 XML 数据到文件
        }
    }
}
